using System.Text.Json.Serialization;
using AgentFrameworkDemo.Models;

namespace AgentFrameworkDemo.Workflows;

/// <summary>
/// Microsoft Agent Framework Workflow模型
/// 基于官方文档架构设计
/// </summary>

/// <summary>
/// Workflow步骤类型
/// </summary>
public enum WorkflowStepType
{
    Task,        // 任务步骤
    Tool,        // 工具调用
    Condition,   // 条件判断
    Loop,        // 循环处理
    Parallel,    // 并行执行
    AgentCall,   // Agent调用
    Delay,       // 延迟
    SubWorkflow  // 子工作流
}

/// <summary>
/// Workflow执行状态
/// </summary>
public enum WorkflowExecutionStatus
{
    Pending,      // 等待执行
    Running,      // 运行中
    Completed,    // 已完成
    Failed,       // 执行失败
    Cancelled,    // 已取消
    Paused        // 已暂停
}

/// <summary>
/// 错误处理策略
/// </summary>
public enum ErrorHandlingPolicy
{
    Stop,         // 停止执行
    Retry,        // 重试
    Continue,     // 继续执行
    Skip          // 跳过当前步骤
}

/// <summary>
/// Workflow定义
/// </summary>
public class WorkflowDefinition
{
    public string Id { get; set; } = Guid.NewGuid().ToString();
    public string Name { get; set; } = string.Empty;
    public string Description { get; set; } = string.Empty;
    public List<WorkflowStep> Steps { get; set; } = new();
    public Dictionary<string, object> Variables { get; set; } = new();
    public ErrorHandlingPolicy DefaultErrorHandling { get; set; } = ErrorHandlingPolicy.Stop;
    public int MaxExecutionTime { get; set; } = 3600; // 1小时
    public int MaxRetries { get; set; } = 3;
    public List<string> Tags { get; set; } = new();
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
    public Dictionary<string, object> Metadata { get; set; } = new();
}

/// <summary>
/// Workflow步骤
/// </summary>
public class WorkflowStep
{
    public string Id { get; set; } = string.Empty;
    public string Name { get; set; } = string.Empty;
    public WorkflowStepType Type { get; set; } = WorkflowStepType.Task;
    public object Configuration { get; set; } = new();
    public List<string> Dependencies { get; set; } = new();
    public ErrorHandlingPolicy ErrorHandling { get; set; } = ErrorHandlingPolicy.Stop;
    public int MaxRetries { get; set; } = 0;
    public int Timeout { get; set; } = 300; // 秒
    public bool ContinueOnError { get; set; } = false;
    public Dictionary<string, object> InputMapping { get; set; } = new();
    public Dictionary<string, object> OutputMapping { get; set; } = new();
    public ConditionSettings? ConditionSettings { get; set; }
    public LoopSettings? LoopSettings { get; set; }
}

/// <summary>
/// 条件设置
/// </summary>
public class ConditionSettings
{
    public string Expression { get; set; } = string.Empty;
    public string TrueStepId { get; set; } = string.Empty;
    public string? FalseStepId { get; set; }
}

/// <summary>
/// 循环设置
/// </summary>
public class LoopSettings
{
    public string LoopType { get; set; } = "While"; // While, For, ForEach
    public string Condition { get; set; } = string.Empty;
    public int MaxIterations { get; set; } = 100;
    public string BodyStepId { get; set; } = string.Empty;
    public Dictionary<string, object> LoopVariables { get; set; } = new();
}

/// <summary>
/// Workflow输入
/// </summary>
public class WorkflowInput
{
    public Dictionary<string, object> Parameters { get; set; } = new();
    public string? SessionId { get; set; }
    public Dictionary<string, object> Context { get; set; } = new();
    public WorkflowExecutionOptions? Options { get; set; }
}

/// <summary>
/// Workflow执行选项
/// </summary>
public class WorkflowExecutionOptions
{
    public int? TimeoutSeconds { get; set; }
    public bool EnableStreaming { get; set; } = false;
    public bool ContinueOnError { get; set; } = false;
    public Dictionary<string, object> GlobalVariables { get; set; } = new();
}

/// <summary>
/// Workflow输出
/// </summary>
public class WorkflowOutput
{
    public WorkflowExecutionStatus Status { get; set; } = WorkflowExecutionStatus.Pending;
    public Dictionary<string, object> Results { get; set; } = new();
    public string? ErrorMessage { get; set; }
    public TimeSpan ExecutionTime { get; set; }
    public List<WorkflowStepExecution> StepExecutions { get; set; } = new();
    public Dictionary<string, object> Metadata { get; set; } = new();
    public WorkflowStatistics Statistics { get; set; } = new();
}

/// <summary>
/// Workflow步骤执行记录
/// </summary>
public class WorkflowStepExecution
{
    public string Id { get; set; } = Guid.NewGuid().ToString();
    public string StepId { get; set; } = string.Empty;
    public WorkflowExecutionStatus Status { get; set; } = WorkflowExecutionStatus.Pending;
    public DateTime StartedAt { get; set; } = DateTime.UtcNow;
    public DateTime? CompletedAt { get; set; }
    public object? Input { get; set; }
    public object? Output { get; set; }
    public string? ErrorMessage { get; set; }
    public int RetryCount { get; set; }
    public TimeSpan ExecutionTime { get; set; }
    public Dictionary<string, object> Metadata { get; set; } = new();
    public List<ToolExecution> ToolExecutions { get; set; } = new();
}

/// <summary>
/// Workflow执行实例
/// </summary>
public class WorkflowExecution
{
    public string Id { get; set; } = Guid.NewGuid().ToString();
    public string WorkflowId { get; set; } = string.Empty;
    public WorkflowExecutionStatus Status { get; set; } = WorkflowExecutionStatus.Pending;
    public WorkflowInput Input { get; set; } = new();
    public WorkflowOutput Output { get; set; } = new();
    public DateTime StartedAt { get; set; } = DateTime.UtcNow;
    public DateTime? CompletedAt { get; set; }
    public Dictionary<string, object> Context { get; set; } = new();
    public List<string> CompletedSteps { get; set; } = new();
    public List<string> FailedSteps { get; set; } = new();
    public List<string> RunningSteps { get; set; } = new();
    public int CurrentAttempt { get; set; } = 1;
    public WorkflowExecutionOptions? Options { get; set; }
}

/// <summary>
/// Workflow统计信息
/// </summary>
public class WorkflowStatistics
{
    public int TotalSteps { get; set; }
    public int CompletedSteps { get; set; }
    public int FailedSteps { get; set; }
    public int SkippedSteps { get; set; }
    public TimeSpan TotalExecutionTime { get; set; }
    public TimeSpan AverageStepExecutionTime { get; set; }
    public Dictionary<string, int> StepTypeCounts { get; set; } = new();
    public Dictionary<string, TimeSpan> StepExecutionTimes { get; set; } = new();
}

/// <summary>
/// Workflow验证结果
/// </summary>
public class WorkflowValidationResult
{
    public bool IsValid { get; set; }
    public List<string> Errors { get; set; } = new();
    public List<string> Warnings { get; set; } = new();
    public List<WorkflowValidationIssue> Issues { get; set; } = new();
    public Dictionary<string, object> Metadata { get; set; } = new();
}

/// <summary>
/// Workflow验证问题
/// </summary>
public class WorkflowValidationIssue
{
    public string Type { get; set; } = string.Empty; // Error, Warning, Info
    public string Code { get; set; } = string.Empty;
    public string Message { get; set; } = string.Empty;
    public string? StepId { get; set; }
    public string? Suggestion { get; set; }
    public Dictionary<string, object> Context { get; set; } = new();
}

/// <summary>
/// 任务步骤配置
/// </summary>
public class TaskStepConfig
{
    public string Task { get; set; } = string.Empty;
    public string? AgentId { get; set; }
    public Dictionary<string, object> Parameters { get; set; } = new();
    public string? Prompt { get; set; }
    public Dictionary<string, object> TaskContext { get; set; } = new();
}

/// <summary>
/// 工具步骤配置
/// </summary>
public class ToolStepConfig
{
    public string ToolId { get; set; } = string.Empty;
    public string ToolName { get; set; } = string.Empty;
    public Dictionary<string, object> Parameters { get; set; } = new();
    public string? ResultVariable { get; set; }
    public bool SaveResultToContext { get; set; } = true;
}

/// <summary>
/// Agent调用步骤配置
/// </summary>
public class AgentCallStepConfig
{
    public string AgentId { get; set; } = string.Empty;
    public string Task { get; set; } = string.Empty;
    public Dictionary<string, object> Parameters { get; set; } = new();
    public Dictionary<string, object> Context { get; set; } = new();
    public bool WaitForCompletion { get; set; } = true;
}

/// <summary>
/// 并行步骤配置
/// </summary>
public class ParallelStepConfig
{
    public List<string> StepIds { get; set; } = new();
    public bool WaitAll { get; set; } = true;
    public int Timeout { get; set; } = 300;
    public ErrorHandlingPolicy ErrorHandling { get; set; } = ErrorHandlingPolicy.Stop;
}

/// <summary>
/// 延迟步骤配置
/// </summary>
public class DelayStepConfig
{
    public TimeSpan Delay { get; set; }
    public string DelayType { get; set; } = "Fixed"; // Fixed, Dynamic
    public string? DynamicExpression { get; set; }
}